package com.sp.pay.action.sms;

import com.sp.pay.common.CacheUtil;
import com.sp.pay.common.DateUtil;
import com.sp.pay.common.RestClient;
import com.sp.pay.dto.company.CompanyDto;
import com.sp.pay.dto.company.CompanyParameterDto;
import com.sp.pay.repository.company.CompanyRepository;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sp.pay.dto.sms.SmsDto;
import com.sp.pay.repository.sms.SmsRepository;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping(value = "serve/")
public class ServeAction {

	private static Log logger = LogFactory.getLog(ServeAction.class);

    @Autowired
    SmsRepository smsRepository;


    @Autowired
    CompanyRepository companyRepository;

    //http://localhost:8080/serve/232/mo.do?userid=23&feecode=12&oid=11&productid=11&fromid=22&rturl=33
	@RequestMapping(value ="{value}/mo.do" )
	public @ResponseBody
    String mo(@PathVariable("value") String value,HttpServletRequest request){
        logger.info("传入参数 value="+value + " request= "+request.getQueryString());

        //获取发送地址
        String company_num = request.getParameter("company_num");
        CompanyDto companyDto = CacheUtil.getCompangy(company_num + "_1");
        if(companyDto==null || companyDto.getId()==0){
            companyDto = companyRepository.queryS_Company(company_num);
            if(companyDto==null || companyDto.getId() == 0){
                return "err";
            }
            CacheUtil.putCompany(company_num+"_1",companyDto);
        }

        //发送请求
        RestClient.post(companyDto.getCallUrl(), request);

        //获取参数
        CompanyParameterDto dto = CacheUtil.getCompanyPara(value+"_2");
        if(dto==null || dto.getId()==0){
            dto = companyRepository.queryS_Para(value);
            if(dto==null || dto.getId() == 0){
                return "err";
            }
            CacheUtil.putCompanyPara(value+"_2",dto);
        }

        //接收数据并保存信息
        SmsDto smsDto = toSmsDto(dto,value,request);
        if(smsDto==null){
            logger.error("未找到对象  传入参数 value="+value + " request= "+request.getQueryString());
            return "err";
        }
        smsRepository.save(smsDto);
        return "ok";
	}

    private SmsDto toSmsDto(CompanyParameterDto dto,String value, HttpServletRequest request){
        SmsDto smsDto = new SmsDto();
        smsDto.setLinkId(request.getParameter(dto.getLinkId()));
        smsDto.setS_company_num(value);

        smsDto = smsRepository.getByLinkIdAndCompanyNum(smsDto);
        if(smsDto==null){
            return null;
        }
        smsDto.setP_addTime(DateUtil.getCurrentDate("yyyy-MM-dd HH:ss:mm"));
        if(StringUtils.isNotEmpty(dto.getParameter_one())){
            smsDto.setP_parameter_one(request.getParameter(dto.getParameter_one()));
        }
        if(StringUtils.isNotEmpty(dto.getParameter_two())){
            smsDto.setP_parameter_two(dto.getParameter_two());
        }
        if(StringUtils.isNotEmpty(dto.getParameter_two())){
            smsDto.setP_parameter_two(dto.getParameter_two());
        }
        if(StringUtils.isNotEmpty(dto.getParameter_three())){
            smsDto.setP_parameter_three(dto.getParameter_three());
        }
        if(StringUtils.isNotEmpty(dto.getParameter_four())){
            smsDto.setP_parameter_four(dto.getParameter_four());
        }
        if(StringUtils.isNotEmpty(dto.getParameter_five())){
            smsDto.setP_parameter_five(dto.getParameter_five());
        }
        if(StringUtils.isNotEmpty(dto.getParameter_six())){
            smsDto.setP_parameter_six(dto.getParameter_six());
        }
        if(StringUtils.isNotEmpty(dto.getParameter_seven())){
            smsDto.setP_parameter_seven(dto.getParameter_seven());
        }
        if(StringUtils.isNotEmpty(dto.getParameter_eight())){
            smsDto.setP_parameter_eight(dto.getParameter_eight());
        }
        if(StringUtils.isNotEmpty(dto.getParameter_nine())){
            smsDto.setP_parameter_nine(dto.getParameter_nine());
        }
        if(StringUtils.isNotEmpty(dto.getParameter_ten())){
            smsDto.setP_parameter_ten(dto.getParameter_ten());
        }
        if(StringUtils.isNotEmpty(dto.getSign())){
            smsDto.setP_sign(dto.getSign());
        }
        return smsDto;
    }
}

